{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gc\n",
    "import os\n",
    "import pickle\n",
    "import pandas as pd\n",
    "from utils.data import *\n",
    "from utils.misc import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATA_DIR = os.path.abspath('../../Data/display_advertising_challenge/processed')\n",
    "USE_QUIZ_SET = False\n",
    "USE_TEST_SET = False\n",
    "USE_FIELD = False\n",
    "USE_HASH = False\n",
    "TRAIN_SAMPLING_RATE = 1.0\n",
    "TEST_SAMPLING_RATE = 1.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "if USE_QUIZ_SET:\n",
    "    train_dataset_type = 'train+valid+test'\n",
    "    test_dataset_type = 'quiz'\n",
    "    \n",
    "elif USE_TEST_SET:\n",
    "    train_dataset_type = 'train+valid'\n",
    "    test_dataset_type = 'test'\n",
    "    \n",
    "else: \n",
    "    train_dataset_type = 'train'\n",
    "    test_dataset_type = 'valid'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_y_train = pd.read_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'y', train_dataset_type]) + '.pkl'))\n",
    "df_X_train = pd.read_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'X', train_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_y_train.index = list(range(df_y_train.shape[0]))\n",
    "df_X_train.index = list(range(df_X_train.shape[0]))\n",
    "\n",
    "if TRAIN_SAMPLING_RATE < 1.0:\n",
    "    df_y_train = df_y_train.sample(frac=TRAIN_SAMPLING_RATE, random_state=42)\n",
    "    df_X_train = df_X_train.loc[df_y_train.index, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in sampled train set: 45840617\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in sampled train set:', df_X_train.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_pipeline = load_pickle(os.path.join(DATA_DIR, '_'.join(['pipeline', train_dataset_type]) + '.pkl'))\n",
    "target_name, num_feature_names, cat_feature_names, n_categories = load_pickle(\n",
    "    os.path.join(DATA_DIR, '_'.join([train_dataset_type, 'metadata.pkl'])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Elapsed time: 24827 sec\n"
     ]
    }
   ],
   "source": [
    "with get_elapsed_time():\n",
    "    model_type = 'ffm' if USE_FIELD else 'fm'\n",
    "    train_dataset_path = os.path.join(DATA_DIR, '_'.join([model_type, 'dataset', train_dataset_type]) + '.libsvm')\n",
    "    dump_libsvm_file(df_X_train, df_y_train, train_dataset_path, num_feature_names, cat_feature_names, \n",
    "                     n_categories, use_field=USE_FIELD, decimals=6, use_hash=USE_HASH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "del df_y_train, df_X_train\n",
    "_ = gc.collect()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_y_test = pd.read_pickle(\n",
    "    os.path.join(DATA_DIR, '_'.join(['df', 'y', test_dataset_type]) + '.pkl')) if not USE_QUIZ_SET else None\n",
    "df_X_test = pd.read_pickle(os.path.join(DATA_DIR, '_'.join(['df', 'X', test_dataset_type]) + '.pkl'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not USE_QUIZ_SET:\n",
    "    df_y_test.index = list(range(df_y_test.shape[0]))\n",
    "df_X_test.index = list(range(df_X_test.shape[0]))\n",
    "\n",
    "if not USE_QUIZ_SET and TEST_SAMPLING_RATE < 1.0:\n",
    "    df_y_test = df_y_test.sample(frac=TEST_SAMPLING_RATE, random_state=42)\n",
    "    df_X_test = df_X_test.loc[df_y_test.index, :]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "# of obs in sampled test set: 6042135\n"
     ]
    }
   ],
   "source": [
    "print('# of obs in sampled test set:', df_X_test.shape[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "with get_elapsed_time():\n",
    "    test_dataset_path = os.path.join(DATA_DIR, '_'.join([model_type, 'dataset', test_dataset_type]) + '.libsvm')\n",
    "    dump_libsvm_file(df_X_test, df_y_test, test_dataset_path, num_feature_names, cat_feature_names, \n",
    "                     n_categories, use_field=USE_FIELD, decimals=6, use_hash=USE_HASH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "if not USE_QUIZ_SET:\n",
    "    del df_y_test, df_X_test\n",
    "    \n",
    "else:\n",
    "    del df_X_test\n",
    "_ = gc.collect()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
